#!/bin/bash

# Running Coffee Machine tests
# Author: Sergey Vartanov (me@enzet.ru, svartanov@ispras.ru)

# Parameters:
#   1 - Test name or "all" for all tests;
#   2 - Path to ThreadSanitizer Offline.

if [ $# -eq 0 ] ; then
    echo 'Please, specify path to ThreadSanitizer Offline executable.'
    exit 0
fi

rm -f log_debug
rm -r logs > log
mkdir logs > log

cmcp="bin:../bin:../lib/bcel-6.0.jar:../lib/args4j-2.0.21.jar:../lib/log4j-1.2.15.jar"

# Classfiles list getting

python ls2cf.py bin classfiles

# Instrumentation

#rm -r coffee_instrument_100
java -cp $cmcp ru.ispras.coffeemachine.core.CoffeeMachine -bin ../bin/ -cc -mt -tcf TestRunner --instrument -iid 100 -CF classfiles

#rm -r coffee_instrument_101
java -cp $cmcp ru.ispras.coffeemachine.core.CoffeeMachine -bin ../bin/ -pi -sd     -tcf TestRunner --instrument -iid 101 -CF classfiles

# Methods list getting (class name - method name - test type)

java -cp bin TestRunner MethodsTests MechanismTests EasyTests CustomTests MediumTests CollectionsTests JUConcurrentTests HighLevelDataRaceTests > methods_list

echo ""
echo "   Result   Test name                                          Description                                                                                        Time"
echo ""

cat methods_list | while read classname
do
	read methodname
	read type
	read description
	read options
	read output
	read exploits

	if [ "$1" == "all" -o "$1" == "$methodname" ]
	then

		begin=$(date +%s)

		description=${description// /_SPACE_}
		output=${output// /_SPACE_}

		# Run ThreadSanitizer Offline for race tests

		if [[ "$type" == 'excluded' ]]
		then
			echo -e '    \033[2mExcl\033[0m    '$methodname
		fi
		
		echo "==============" >> log_debug
		echo "$classname $methodname" >> log_debug

		if [ "$type" == 'race' -o "$type" == 'norace' ]
		then
			java -cp $cmcp ru.ispras.coffeemachine.core.CoffeeMachine -bin ../bin/ -cc -mt -tcf MethodRunner -iid 100 -CF classfiles $classname $methodname > logs/cm_log_$methodname

			cat logs/cm_log_$methodname >> log_debug

			python check.py logs/cm_log_$methodname "" "Exception"

			cat code-positions > cm.events
			cat events >> cm.events

			cat cm.events > logs/events_$methodname

			python verify_trace.py cm.events

			$2 < cm.events &> logs/tsan_answer_$methodname

			if [[ "$type" == 'race' ]]
			then
				python check_output.py logs/tsan_answer_$methodname "WARNING:" "error" $methodname $description > check
			else
				python check_output.py logs/tsan_answer_$methodname "" "WARNING:" $methodname $description > check
			fi

			cat check

			end=$(date +%s)
			diff=$(($end - $begin))
			echo " $diff"
		fi

		if [[ "$type" == 'coffee' ]]
		then
      cat classfiles > classfiles2
      echo "!java.io.FileInputStream.class" >> classfiles2
      echo "  read ([B)I\n" >> classfiles2

			java -cp $cmcp ru.ispras.coffeemachine.core.CoffeeMachine -bin ../bin/ -pi -sd -tcf MethodRunner -iid 101 -CF classfiles2 $options $classname $methodname > logs/cm_log_$methodname

			cat logs/cm_log_$methodname >> log_debug

			if [[ "$output" != '-' ]]
			then
				python check_output.py logs/cm_log_$methodname "$output" "error" $methodname $description > check
			fi

			if [[ "$exploits" != '-1' ]]
			then
				python check_output.py logs/cm_log_$methodname "exploit" "error" $methodname $description > check
			fi

			cat check

			end=$(date +%s)
			diff=$(($end - $begin))
			echo " $diff"
		fi
	fi

	trap "exit" SIGINT
done

rm check classfiles
#rm methods_list 

echo ""
